package com.wosai.register.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wosai.register.model.OrderItem;

public class JsonUtil {

    public static byte[] toJson(Object value) {

        byte[] data;

        try {
            ObjectMapper mapper = new ObjectMapper();
            data = mapper.writeValueAsBytes(value);
        }
        catch (JsonProcessingException e) {
            data = "\"Json serialization error\"".getBytes();
        }

        return data;
    }
    
    public static List<OrderItem> orderItemsFromJson(byte[] src) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            List<OrderItem> value = mapper.readValue(src, new TypeReference<List<OrderItem>>(){});
            return value;
        }
        catch (IOException e) {
            return null;
        }
        
    }
    
}
